added Feb 2001 SDK
[windows-sources.git] / shared source / wpf / src / host / shared / stringmap.hxx
blobeb4227d2708b65322675d55ae6122d4268ff43da
1 //------------------------------------------------------------------------
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // Description:
6 // Defines a list convenience class, for use with a small number of
7 // elements.
8 //
9 // History:
10 // 2005/06/19 - [....]
11 // Created
12 // 2007/09/20-[....]
13 // Ported Windows->DevDiv. See SourcesHistory.txt.
15 //------------------------------------------------------------------------
17 #pragma once
19 #ifndef STRINGMAP_HXX_INCLUDED
20 #define STRINGMAP_HXX_INCLUDED
22 #include "string.hxx"
23 #include "miscmacros.hxx"
25 template<class T>
26 class CStringMap
28 public:
29 class CStringMapNode
31 public:
32 CStringMapNode(CStringMapNode* pNext, LPCWSTR key, T tValue)
34 m_pNext = pNext;
35 SetKey(key);
36 m_tValue = tValue;
38 STRING_PROP(Key);
39 T GetValue() { return m_tValue; }
40 CStringMapNode* GetNext() { return m_pNext; }
42 private:
43 CString m_strKey;
44 T m_tValue;
45 CStringMapNode* m_pNext;
48 public:
49 CStringMap()
51 m_pRoot = NULL;
52 m_dwCount = 0;
55 ~CStringMap()
57 CStringMapNode* pNode = m_pRoot;
59 while (pNode)
61 CStringMapNode* pNext = pNode->GetNext();
62 delete pNode;
63 pNode = pNext;
67 public:
68 HRESULT Add(LPCWSTR pwzKey, T tValue)
70 HRESULT hr = S_OK;
72 if (!pwzKey)
74 CKHR(E_INVALIDARG);
77 m_pRoot = new CStringMapNode(m_pRoot, pwzKey, tValue);
78 CK_ALLOC(m_pRoot);
79 ++m_dwCount;
81 Cleanup:
82 return hr;
85 HRESULT Find(LPCWSTR pwzKey, T* ptValue)
87 HRESULT hr = S_OK;
89 CStringMapNode* pNode = m_pRoot;
91 while (pNode)
93 if (_wcsicmp(pwzKey, pNode->GetKey()) == 0)
95 *ptValue = pNode->GetValue();
96 break;
99 pNode = pNode->GetNext();
102 if (!pNode)
104 CKHR(E_FAIL);
107 Cleanup:
108 return hr;
111 CStringMapNode* GetRoot() { return m_pRoot; }
113 DWORD GetCount() { return m_dwCount; }
115 private:
116 CStringMapNode* m_pRoot;
117 DWORD m_dwCount;
120 #endif //STRINGMAP_HXX_INCLUDED